package com.whfc.mach.queue;

import com.whfc.common.constant.QueueConst;
import com.whfc.common.util.JSONUtil;
import com.whfc.mach.dao.AppMachWarnRuleObjectMapper;
import com.whfc.mach.dto.warn.MachWarnCheckDTO;
import com.whfc.mach.dto.warn.MachWarnRuleDTO;
import com.whfc.mach.manager.warn.AppWarnManager;
import com.whfc.mach.manager.warn.AppWarnManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author sun_guodong
 * @description 報警隊列監聽
 * @date 2020-05-22
 */
@Component
@RabbitListener(queues = QueueConst.ALARM_CHECK_DATA)
public class WarnCheckDataQueueMessageListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppWarnManagerFactory appWarnManagerFactory;

    @Autowired
    private AppMachWarnRuleObjectMapper appMachWarnRuleObjectMapper;

    @RabbitHandler
    public void process(String textMsg) {
        logger.debug("push_warn_listener:{}", textMsg);
        try {
            MachWarnCheckDTO machWarnCheckDTO = JSONUtil.parseObject(textMsg, MachWarnCheckDTO.class);
            // 获取检测规则集合
            List<MachWarnRuleDTO> appWarnRuleList = appMachWarnRuleObjectMapper.selectByObjectId(String.valueOf(machWarnCheckDTO.getMachId()));
            for (MachWarnRuleDTO machWarnRuleDTO : appWarnRuleList) {
                try {
                    AppWarnManager warnCheckManager = appWarnManagerFactory.getWarnCheckManager(machWarnRuleDTO.getRuleType());
                    if (warnCheckManager != null) {
                        warnCheckManager.checkWarn(machWarnCheckDTO, machWarnRuleDTO);
                    }
                } catch (Exception e) {
                    logger.error("检测报警出错,appWarnRuleDTO:{},e:{}", machWarnRuleDTO.toString(), e);
                    continue;
                }
            }
        } catch (Exception e) {
            logger.error("处理监听失败push_warn_listener:{}:::::e:{}", textMsg, e);
        }
    }


}
